package com.jinzhi.jzweb.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.*;
import com.jinzhi.jzweb.pojo.EvaluationExportVO;
import com.jinzhi.jzweb.pojo.ExaminationResultsExportAllVo;
import com.jinzhi.jzweb.pojo.ExaminationResultsExportVo;
import com.jinzhi.jzweb.pojo.TrainStudentInfoExportVo;
import com.jinzhi.jzweb.service.*;
import com.jinzhi.jzweb.utils.EasyPoiUtil;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.*;

/**
 * <pre>
 * 考试成绩
 * </pre>
 * <small> 2020-02-24 21:00:12 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/examinationResults")
public class ExaminationResultsController extends BaseController {
    @Autowired
    private ExaminationResultsService examinationResultsService;
    @Autowired
    private ExaminationBatchService examinationBatchService;
    @Autowired
    private CertificationBatchService certificationBatchService;
    @Autowired
    private StuSignUpService stuSignUpService;
    @Autowired
    private EvaluationService evaluationService;

    @GetMapping()
    @RequiresPermissions("jzweb:examinationResults:examinationResults")
    String ExaminationResults() {
        return "jzweb/examinationResults/examinationResults";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("jzweb:examinationResults:examinationResults")
    public Result<Page<ExaminationResultsDO>> list(HttpServletRequest req ,ExaminationResultsDO examinationResultsDTO, int pageSize, int pageNumber) {
        //查询用户角色
        String roleName = getRoleNames();
        if (!roleName.contains("超级用户角色")) {
            Object orgId = req.getSession().getAttribute("orgId");
            Map map = new HashMap();
            if (orgId != null){
                map.put("orgId",orgId);
            }else {
                map.put("orgId",this.getUser().getOrgId());
            }
            map.put("cbatchId",examinationResultsDTO.getCbatchId());
            map.put("ebatchId",examinationResultsDTO.getEbatchId());
            map.put("stuSignId",examinationResultsDTO.getStuSignId());
            map.put("state",examinationResultsDTO.getState());
            Page<ExaminationResultsDO> page = examinationResultsService.findPageList(map, pageNumber, pageSize);

            List<ExaminationResultsDO> list = page.getRecords();
            for (ExaminationResultsDO exam : list) {
                if (exam.getCbatchId() != null) {
                    CertificationBatchDO certificationBatchDO = certificationBatchService.selectById(exam.getCbatchId());
                    if (certificationBatchDO != null) {
                        exam.setCbatchName(certificationBatchDO.getName());
                    }
                }

                if (exam.getEbatchId() != null) {
                    ExaminationBatchDO examinationBatchDO = examinationBatchService.selectById(exam.getEbatchId());
                    if (examinationBatchDO != null) {
                        if (examinationBatchDO.getType().equals(0)) {
                            exam.setEbatchName(examinationBatchDO.getName() + "--理论考试");
                        } else if (examinationBatchDO.getType().equals(1)) {
                            exam.setEbatchName(examinationBatchDO.getName() + "--技能考试");
                        } else if (examinationBatchDO.getType().equals(2)) {
                            exam.setEbatchName(examinationBatchDO.getName() + "--社会实践");
                        } else {
                            exam.setEbatchName(examinationBatchDO.getName());
                        }
                    }
                }

                if (exam.getStuSignId() != null) {
                    StuSignUpDO stuSignUpDO = stuSignUpService.selectById(exam.getStuSignId());
                    if (stuSignUpDO != null) {
                        exam.setStuName(stuSignUpDO.getName());
                        EvaluationDO ed = evaluationService.findOneByKv("admission_number",stuSignUpDO.getTicketNumber());
                        if(ed!=null){
                            exam.setPracticalOperation(ed.getPracticalOperation().toString());
                        }else{
                            exam.setPracticalOperation("无");
                        }
                    }
                }
            }
            page.setRecords(list);
            return Result.ok(page);
        } else {
            Page<ExaminationResultsDO> page = examinationResultsService.selectPage(getPage(ExaminationResultsDO.class),
                    examinationResultsService.convertToEntityWrapper("cbatch_id", examinationResultsDTO.getCbatchId() , "ebatch_id", examinationResultsDTO.getEbatchId(),
                            "stu_sign_id", examinationResultsDTO.getStuSignId(),"state",examinationResultsDTO.getState()).orderBy("id", false));
            List list = new ArrayList();
            for (int i = 0; i < page.getRecords().size(); i++) {
                ExaminationResultsDO obj = (ExaminationResultsDO) page.getRecords().get(i);

                if (obj.getCbatchId() != null) {
                    CertificationBatchDO certificationBatchDO = certificationBatchService.selectById(obj.getCbatchId());
                    if (certificationBatchDO != null) {
                        obj.setCbatchName(certificationBatchDO.getName());
                    }
                }
                if (obj.getEbatchId() != null) {
                    ExaminationBatchDO examinationBatchDO = examinationBatchService.selectById(obj.getEbatchId());
                    if (examinationBatchDO != null) {
                        if (examinationBatchDO.getType().equals(0)) {
                            obj.setEbatchName(examinationBatchDO.getName() + "--理论考试");
                        } else if (examinationBatchDO.getType().equals(1)) {
                            obj.setEbatchName(examinationBatchDO.getName() + "--技能考试");
                        } else if (examinationBatchDO.getType().equals(2)) {
                            obj.setEbatchName(examinationBatchDO.getName() + "--社会实践");
                        } else {
                            obj.setEbatchName(examinationBatchDO.getName());
                        }
                    }
                }
                if (obj.getStuSignId() != null) {
                    StuSignUpDO stuSignUpDO = stuSignUpService.selectById(obj.getStuSignId());
                    if (stuSignUpDO != null) {
                        obj.setStuName(stuSignUpDO.getName());
                        EvaluationDO ed = evaluationService.findOneByKv("admission_number",stuSignUpDO.getTicketNumber());
                        if(ed!=null){
                            obj.setPracticalOperation(ed.getPracticalOperation().toString());
                        }else{
                            obj.setPracticalOperation("无");
                        }

                    }
                }

                list.add(obj);
            }
            page.setRecords(list);
            return Result.ok(page);
        }
    }
    
    
    @GetMapping("/exportList")
    @ResponseBody
    @Transactional
    @RequiresPermissions("jzweb:examinationResults:exportList")
    public void exportlist(HttpServletRequest request, HttpServletResponse response,String ebatchId) throws Exception{
    	Map map = new HashMap();
    	
    	if(ebatchId==null || ebatchId=="")
    		return ;
        map.put("cbatchId",null);
        map.put("ebatchId",ebatchId);
        List<ExaminationResultsExportVo> examinationResults = examinationResultsService.exportList(map);
        if(examinationResults.size()<=0)
        	return;
        EasyPoiUtil.exportExcel(examinationResults, "考试成绩", "考试成绩", ExaminationResultsExportVo.class,examinationResults.get(0).getEbatchName()+"-考试成绩表", response);
    }

    @GetMapping("/exportListAll")
    @ResponseBody
    @Transactional
    @RequiresPermissions("jzweb:examinationResults:exportList")
    public void exportlistAll(HttpServletRequest request, HttpServletResponse response,String ebatchId) throws Exception{
        Map map = new HashMap();

        if(ebatchId==null || ebatchId=="")
            return ;
        map.put("cbatchId",null);
        map.put("ebatchId",ebatchId);
        List<ExaminationResultsExportAllVo> examinationResults = examinationResultsService.exportListAll(map);
        if(examinationResults.size()<=0)
            return;
        EasyPoiUtil.exportExcel(examinationResults, "考试成绩", "考试成绩", ExaminationResultsExportAllVo.class,examinationResults.get(0).getEbatchName()+"-考试成绩表", response);
    }

    @GetMapping("/add")
    @RequiresPermissions("jzweb:examinationResults:add")
    String add() {
        return "jzweb/examinationResults/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("jzweb:examinationResults:edit")
    String edit(@PathVariable("id") Long id, Model model) {
        ExaminationResultsDO examinationResults = examinationResultsService.selectById(id);
        model.addAttribute("examinationResults", examinationResults);
        return "jzweb/examinationResults/edit";
    }

    @Log("添加考试成绩")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("jzweb:examinationResults:add")
    public Result<String> save(ExaminationResultsDO examinationResults) {
        examinationResultsService.insert(examinationResults);
        return Result.ok();
    }

    @Log("修改考试成绩")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("jzweb:examinationResults:edit")
    public Result<String> update(ExaminationResultsDO examinationResults) {
        boolean update = examinationResultsService.updateById(examinationResults);
        return update ? Result.ok() : Result.fail();
    }

    @Log("删除考试成绩")
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("jzweb:examinationResults:remove")
    public Result<String> remove(Long id) {
        examinationResultsService.deleteById(id);
        return Result.ok();
    }

    @Log("批量删除考试成绩")
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("jzweb:examinationResults:batchRemove")
    public Result<String> remove(@RequestParam("ids[]") Long[] ids) {
        examinationResultsService.deleteBatchIds(Arrays.asList(ids));
        return Result.ok();
    }


    /**
     * @Description: 查询认证批次信息并返回，用于页面筛选条件的下拉框选值；
     * 参数：显示数，当前页数，认证批次名称
     * @Param: [current, number, name, cbatchId]
     * @return: com.jinzhi.common.utils.Result<?>
     * @Author: kl
     * @Date: 2020-04-13
     */
    @Log("查询认证批次信息")
    @GetMapping("/selCbatch")
    @ResponseBody
    public Result<?> selCbatch(int pageNumber, int pageSize, String name) {
        String roleName = getRoleNames();
        if (!roleName.contains("超级用户角色")) {
            Page<Map<Object, Object>> mapru = certificationBatchService.selCbatchList(pageNumber, pageSize, getUser().getOrgName());
            return Result.ok(mapru);
        }else{
            Page<Map<Object, Object>> mapru = certificationBatchService.selCbatchList(pageNumber, pageSize, name);
            return Result.ok(mapru);
        }


    }

    /**
     * @Description: 查询考试批次信息并返回，用于页面筛选条件的下拉框选值；
     * 参数：显示数，当前页数，考试批次名称，认证批次id
     * @Param: [current, number, name, cbatchId]
     * @return: com.jinzhi.common.utils.Result<?>
     * @Author: kl
     * @Date: 2020-04-13
     */
    @Log("查询考试批次信息")
    @GetMapping("/selEbatch")
    @ResponseBody
    public Result<?> selEbatch(int pageNumber, int pageSize, String name, String cbatchId) {
        String roleName = getRoleNames();
        if (!roleName.contains("超级用户角色")) {
            Page<Map<Object, Object>> mapru = examinationBatchService.selByNameCbatchId(pageNumber, pageSize, getUser().getOrgName(), cbatchId);
            return Result.ok(mapru);
        }else{
            Page<Map<Object, Object>> mapru = examinationBatchService.selByNameCbatchId(pageNumber, pageSize, name, cbatchId);
            return Result.ok(mapru);
        }


    }


	/**
	 * @Description: 查询考生信息并返回，用于页面筛选条件的下拉框选值；
	 * 参数：当前页数，显示数，考生姓名，认证批次id
	 * @Param: [current, number, name, cbatchId]
	 * @return: com.jinzhi.common.utils.Result<?>
	 * @Author: kl
	 * @Date: 2020-04-13
	 */
    @Log("查询考生信息")
    @GetMapping("/selStuSign")
    @ResponseBody
    public Result<?> selStuSign(int pageNumber, int pageSize, String name, String cbatchId) {
        Map map = new HashMap();
        map.put("name",name);
        if(StringUtils.isNotBlank(cbatchId)){
            map.put("cbatchId",Long.parseLong(cbatchId));
        }else{
            map.put("cbatchId","");
        }
        Page<Map<Object, Object>> mapru = stuSignUpService.selByNameCbatchId(pageNumber, pageSize, map);
        return Result.ok(mapru);
    }
}
